package com.mt.roll.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.GradeInformation;
import com.mt.roll.core.dal.entity.WorkOrder;
import com.mt.roll.core.dal.entity.WorkOrderExecute;
import com.mt.roll.core.dal.service.ProcessFlowServiceBiz;
import com.mt.roll.core.dal.service.WorkOrderExecuteServiceBiz;
import com.mt.roll.core.dal.service.WorkOrderServiceBiz;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.wms.params.WorkOrderParam;
import com.mt.roll.wms.params.WorkOrderQueryParam;
import com.mt.roll.wms.service.WorkOrderService;
import com.mt.roll.wms.vo.WorkOrderVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @auther why
 * @date 2021/10/18 9:18
 */
@Service
@Transactional
public class WorkOrderServiceImpl extends BaseService implements WorkOrderService {

    @Autowired
    private WorkOrderServiceBiz workOrderServiceBiz;

    @Autowired
    private WorkOrderExecuteServiceBiz workOrderExecuteServiceBiz;
    @Autowired
    private ProcessFlowServiceBiz processFlowServiceBiz;

    @Override
    public R<PageVo<WorkOrder>> page(WorkOrderQueryParam workOrderQueryParam) {
        QueryWrapper<WorkOrder> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(workOrderQueryParam.getName()), WorkOrder.NAME, workOrderQueryParam.getName())
                .orderByDesc(WorkOrder.ID);
        IPage<WorkOrder> page = workOrderServiceBiz.page(new Page<>(workOrderQueryParam.getCurrent(), workOrderQueryParam.getSize()), wrapper);
        return successful(new PageVo<>(page, WorkOrder.class));
    }

    @Override
    public R<WorkOrder> get(IdParam idParam) {
        WorkOrder workOrder = workOrderServiceBiz.getById(idParam.getId());
        Assert.notNull("工单不存在", workOrder);
        WorkOrderVo workOrderVo = WorkOrderVo.builder().build();
        BeanUtils.copyProperties(workOrder, workOrderVo);
        return successful(workOrder);
    }

    @Override
    public R<IdVo> update(WorkOrderParam workOrderParam) {
        WorkOrder workOrder = new WorkOrder();
        workOrder.setId(workOrderParam.getId());
        workOrder.setProcessId(workOrderParam.getSubProcessId());
        String name = processFlowServiceBiz.getById(workOrderParam.getSubProcessId()).getName();
        workOrder.setSubProccessName(name);
        workOrderServiceBiz.updateById(workOrder);
        return successful(IdVo.builder().id(workOrder.getId()).build());
    }

    @Override
    public R<IdVo> oobToOoaWorkOrder(WorkOrderParam workOrderParam) {
        WorkOrder workOrder = new WorkOrder();
        BeanUtils.copyProperties(workOrderParam, workOrder);
        setCommonField(workOrder);
        workOrderServiceBiz.save(workOrder);

        WorkOrderExecute workOrderExecute = new WorkOrderExecute();
        BeanUtils.copyProperties(workOrderParam,workOrder);
        setCommonField(workOrderExecute);
        workOrderExecuteServiceBiz.save(workOrderExecute);

        return successful(IdVo.builder().id(workOrder.getId()).build());
    }
}
